<template>
  <div>
    <items-head title="更新联系人" />
    <div class="wrap">
      <van-address-edit :area-list="areaList"
                        :address-info="addressInfo"
                        show-postal
                        show-delete
                        show-set-default
                        :area-columns-placeholder="['请选择', '请选择', '请选择']"
                        @save="onSave"
                        @delete="onDelete" />
    </div>
  </div>
</template>

<script>
import ItemsHead from '../../components/ItemsHead.vue'
import { areaList } from '@vant/area-data'
import { UpdateContacts, getContent, DelContacts } from 'api'
import { getToken } from 'utils'
export default {
  name: 'UpdateContacts',
  data () {
    return {
      areaList,
      addressInfo: {

      }
    }
  },
  created () {
    this.getContent()
  },
  methods: {
    onDelete () {
      DelContacts({
        id: this.$route.query.id,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.$toast.success({
            message: '删除成功',
            duration: 500,
            onClose: () => {
              this.$router.push('/contlist')
            }
          })
        }
      })
    },
    getContent () {
      getContent({
        id: this.$route.query.id,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          const { id, linkMan, mobile, address, isDefault, cityStr } = res.data.data.info
          this.addressInfo = {
            id,
            name: linkMan,
            tel: mobile,
            address,
            isDefault,
            county: cityStr
          }
        }
      })
    },
    onSave (params) {
      const { addressDetail, postalCode, name, tel, areaCode, isDefault } = params
      params = {
        id: this.$route.query.id,
        address: addressDetail,
        cityId: postalCode,
        linkMan: name,
        mobile: tel,
        provinceId: areaCode,
        isDefault,
        token: getToken()
      }
      UpdateContacts(params).then(res => {
        if (res.data.code === 0) {
          this.$toast.success({
            message: '修改成功',
            duration: 500,
            onClose: () => {
              this.$router.push('/contlist')
            }
          })
        }
      })
    }
  },
  components: { ItemsHead }

}
</script>

<style lang="scss" scoped>
</style>
